home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 February / EnigmA AMIGA RUN 15 (1997)(G.R. Edizioni)(IT)[!][issue 1997-02][PLANET CD V].iso / enigma / onlypc / java / samples.bin / Client.java < prev    next >
Text File  |  1996-09-04  |  6KB  |  225 lines

  1. /*
  2.  * Title: Conversing Applets
  3.  * Type: Applet
  4.  * Source: Client.java
  5.  * Application Description:
  6.  * The Conversing Applets project provides a simple and effective way
  7.  * to allow applets to communicate within the same "environment", i.e.
  8.  * within the same browser page.
  9.  *
  10.  * This is done using a Mediator class as the background manager and
  11.  * other visible classes which display results. The Mediator class uses
  12.  * static variables to keep track of all the events taking place in
  13.  * these visible applets.
  14.  *
  15.  * In this example, two visible applets are used along with the one
  16.  * Mediator class. The Developer and Client class generate text strings which
  17.  * represent conversation.  The Mediator object(s) take those text
  18.  * strings and distribute them to the other applet (Developer to Client and
  19.  * Client to Developer).
  20.  *
  21.  * The result is two distinct applet objects transmitting data back and
  22.  * forth. This sort of project can be very useful in developing more
  23.  * interesting Web pages as well as Web page applets which gather
  24.  * information about usage, etc.  The Mediator class concept can be
  25.  * built upon to meet the needs of more specific Java-based Web
  26.  * applications.
  27.  *
  28.  * Symantec Corporation.
  29.  */
  30.  
  31.  
  32. import java.applet.Applet;
  33. import java.awt.*;
  34.  
  35. /**
  36.  * The Client applet class
  37.  */
  38.  
  39. public class Client extends Applet implements Runnable
  40. {
  41.         Mediator myMediator;            //Local copy of the Mediator object
  42.         String label, response;         //Various applet text variables
  43.         int theNumber;                  //Used to track a random number
  44.         Thread clientThread;             //The main applet thread
  45.  
  46.  
  47.         /**
  48.          * Set up the applet layout and initialize variables
  49.          */
  50.  
  51.         public void init()
  52.         {
  53.            myMediator = new Mediator();
  54.            setLayout(new GridLayout(5,1));
  55.  
  56.            //{{INIT_CONTROLS
  57.         setLayout(new FlowLayout(FlowLayout.CENTER,5,5));
  58.         addNotify();
  59.         resize(189,128);
  60.         top = new java.awt.Label("I'm a Client");
  61.         top.reshape(19,20,67,13);
  62.         add(top);
  63.         client = new java.awt.Label("Client says:");
  64.         client.reshape(19,33,70,13);
  65.         add(client);
  66.         clientTalk = new java.awt.TextField(18);
  67.         clientTalk.reshape(19,46,150,18);
  68.         add(clientTalk);
  69.         developer = new java.awt.Label("The Developer just said:");
  70.         developer.reshape(19,70,108,15);
  71.         add(developer);
  72.         developerTalk = new java.awt.TextField(17);
  73.         developerTalk.reshape(19,90,148,18);
  74.         add(developerTalk);
  75.         //}}
  76.  
  77.            developerTalk.setEditable(false);
  78.            clientTalk.setEditable(false);
  79.            clientTalk.setForeground(Color.white);
  80.  
  81.         }
  82.  
  83.  
  84.         /**
  85.          * Start the main thread
  86.          */
  87.  
  88.         public void start()
  89.         {
  90.            clientThread = new Thread(this);
  91.            clientThread.start();
  92.         }
  93.  
  94.  
  95.         /**
  96.          * Handle any events here
  97.          */
  98.  
  99.         public boolean handleEvent(Event evt)
  100.         {
  101.             if(evt.target instanceof Button)
  102.             {
  103.                return true;
  104.             }
  105.            return false;
  106.          }
  107.  
  108.  
  109.          /**
  110.           * The applet action takes place here. The applet waits, gets a
  111.           * random number from the Mediator, uses that to id a piece of
  112.           * conversation and then updates the UI elements.
  113.           */
  114.  
  115.          public void run()
  116.          {
  117.             Thread clientThread = Thread.currentThread();
  118.             while(true)
  119.             {
  120.                 try
  121.                 {
  122.                     clientThread.sleep(2000);
  123.                 } catch(Exception e)
  124.                 {
  125.                     System.out.println(e);
  126.                 }
  127.  
  128.                 //Keep a copy of the last random number
  129.                 myMediator.oldNumber = myMediator.theNumber;
  130.                 myMediator.theNumber = myMediator.genRandom();
  131.  
  132.                 checkWithMediator();
  133.  
  134.                 takeAction();
  135.  
  136.                 //System.out.println("response =" + response);
  137.             }
  138.          }
  139.  
  140.  
  141.          /**
  142.           * A helper function which uses the new Mediator random number
  143.           * to set the conversation text
  144.           */
  145.  
  146.          public synchronized void checkWithMediator()
  147.          {
  148.  
  149.             int index = myMediator.theNumber;
  150.  
  151.             System.out.println(index);
  152.  
  153.             if(index < 10)
  154.             {
  155.                 label = "Isn't Café great?";
  156.             } else if((index <= 20) && (index >= 10))
  157.             {
  158.                 label = "The Café compiler is sooo FAST!";
  159.             } else if((index <= 30) && (index > 20))
  160.             {
  161.                 label = "Check out the snazzy class editor.";
  162.             } else if((index <= 40) && (index > 30))
  163.             {
  164.                 label = "Debugging in Café is easy.";
  165.             } else if((index <= 50) && (index > 40))
  166.             {
  167.                 label = "Café is Great!";
  168.             } else if((index <= 60) && (index > 50))
  169.             {
  170.                 label = "What do you mean by that?";
  171.             } else if((index <= 70) && (index > 60))
  172.             {
  173.                 label = "I want to make sure you use Café.";
  174.             } else if((index <= 80) && (index > 70))
  175.             {
  176.                 label = "Isn't that the truth";
  177.             } else if((index <= 90) && (index > 80))
  178.             {
  179.                 label = "Wow, really!!!";
  180.             } else if((index <= 100) && (index >90))
  181.             {
  182.                 label = "That's very cool";
  183.             }
  184.  
  185.             //Let the Mediator know what the client said
  186.             myMediator.setClientTalk(label);
  187.  
  188.             //Find out what the developer said
  189.             response = myMediator.getDeveloperTalk();
  190.          }
  191.  
  192.  
  193.  
  194.          /**
  195.           * A helper method which updates the UI elements
  196.           */
  197.  
  198.          public void takeAction()
  199.          {
  200.             clientTalk.setText(label);
  201.             developerTalk.setText(response);
  202.          }
  203.  
  204.         //{{DECLARE_CONTROLS
  205.     java.awt.Label top;
  206.     java.awt.Label client;
  207.     java.awt.TextField clientTalk;
  208.     java.awt.Label developer;
  209.     java.awt.TextField developerTalk;
  210.     //}}
  211. }
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.